﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "protocol/serial.h"
#include "controller.h"
#include "usbbox.h"
#include <QMainWindow>
#include <QLabel>
#include <QCheckBox>
#include <qcustomplot/qcustomplot.h>
#include "plot/plotmanage.h"
#include "dialog_mc_update.h"
#include "dialog_log.h"
#define GCP_CUSTOM_LINE_COLORS 4

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    void ShowPermanentMessage(QString message, QColor clr = Qt::blue);
    Q_INVOKABLE void ShowMessage(QString message, QColor clr = Qt::green);
    void PlotInit();
private slots:

    void replot();

    void on_pushButton_serialFlush_clicked();

    void on_pushButton_serialOpen_clicked();

    void on_checkBox_direct_stateChanged(int arg1);

    void onMotorStsChanged(QString);
    void onPlatData(unsigned mask, float p1, float p2, float p3, float p4);
    void on_checkBox_phase_curr_stateChanged(int arg1);

    void on_checkBox_phase_vol_stateChanged(int arg1);

    void on_checkBox_in_torque_stateChanged(int arg1);

    void on_checkBox_out_torque_stateChanged(int arg1);

    void on_checkBox_in_vel_stateChanged(int arg1);

    void on_checkBox_mot_vel_stateChanged(int arg1);

    void on_checkBox_mot_angle_stateChanged(int arg1);

    void on_checkBox_dq_current_stateChanged(int arg1);

    void on_spinAxesMin_valueChanged(int arg1);

    void on_spinAxesMax_valueChanged(int arg1);

    void on_spinYStep_valueChanged(int arg1);

    void on_spinPoints_valueChanged(int arg1);

    void OnMouse_wheel_in_plot(QWheelEvent *event);

    void onMouseMoveInPlot(QMouseEvent *event);

    void onMouseDoubleClick(QMouseEvent *event);

    void channel_selection (void);

    void legend_double_click(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event);

    void on_pushButton_AutoScale_clicked();

    void on_checkBox_dq_vol_stateChanged(int arg1);

    void on_pushButton_m_start_clicked();

    void on_pushButton_m_stop_clicked();

    void on_checkBox_debug_stateChanged(int arg1);

    void on_pushButton_D_Aligne_clicked();

    void on_pushButton_D_turn_clicked();

    void on_pushButton_enc_zero_cali_clicked();

    void on_pushButton_ctrlmode_clicked();

    void on_checkBox_dq_curr_target_stateChanged(int arg1);

    void on_checkBox_svpwm_stateChanged(int arg1);

    void on_lineEdit_input_vel_returnPressed();

    void on_lineEdit_Mot_Enc_off_returnPressed();

    void on_checkBox_dc_curr_stateChanged(int arg1);

    void my_menu_display(const QPoint &pos);

    void on_checkBox_fw_en_stateChanged(int arg1);

    void on_pushButton_clicked();

    void on_pushButton_vdq_clicked();

    void on_pushButton_torque_clicked();

    void on_pushButton_dqcurr_clicked();

protected:
    void closeEvent(QCloseEvent *event) override;
private:
    void motStsChanged(QString);
    bool CheckSelectedBox(QCheckBox* box, bool checked);
    void InitCheckBoxProp();
    void UpdateMCParamToUI();
    void UpdateMCParamFromUI();
    void UpdateAppParamToUI();
    void UpdateAppParamFromUI();
    void ShowEditText(QLineEdit *edit, float v);
    void ShowEditText(QLineEdit *edit, s16 v);
    void ShowEditText(QLineEdit *edit, bool v);
    void ShowEditText(QLineEdit *edit, u16 v);
    void ShowEditText(QLineEdit *edit, u8 v);
    s16  getValue_s16(QLineEdit *edit);
    float getValue_float(QLineEdit *edit);
    u16  getValue_u16(QLineEdit *edit);
    u8  getValue_u8(QLineEdit *edit);
private:
    Ui::MainWindow *ui;
    QLabel *PermanentLab;
    PlotManage *plotMan;
    QTimer updateTimer;
    QMenu *right_menu;
    Serial *serial;
    bool b_serial_opend;
    u32  life_time;
    Controller *mc;
    UsbBox     *box;
    QColor gui_colors[GCP_CUSTOM_LINE_COLORS];
    bool   open_force;
    Dialog_mc_update *mc_update;
    Dialog_mc_update *box_update;
    Dialog_log       *mc_logger;
};
#endif // MAINWINDOW_H
